package br.ufal.ic.fracao.agent.resolution;

import java.io.IOException;
import java.util.List;

import org.jdom.JDOMException;

import br.ufal.ic.fracao.especialist.domain.Item;
import br.ufal.ic.fracao.especialist.inference.InferenceAlgorithm;

public class Resolution {

	private boolean multiplication;
	private boolean additionSubtraction;
	private boolean simplification;
	private boolean exponentiation;
	
	public Resolution(List value){
		this.updateValue(value);
	}
	
	public void updateValue(List value){
		if(value.contains("*")){
			this.multiplication = true;
		}
		if(value.contains("^")){
			this.exponentiation = true;
		}
		if(value.contains("+") || value.contains("-")){
			this.additionSubtraction = true;
		}
		if(!value.contains("+") && !value.contains("-") && !value.contains("*")){
			this.simplification = true;
		}		
	}
	
	public String getOperation() throws IOException, JDOMException{
		InferenceAlgorithm algorithm = new InferenceAlgorithm();
		
		Item fato1 = new Item();
        fato1.setDescription("MultiplicationOperation");
        fato1.setValue(String.valueOf(this.multiplication));
        fato1.setCertaintyFactor(0.2);
        algorithm.addToMemoryWorking(fato1);
        
        Item fato2 = new Item();
        fato2.setDescription("AdditionSubtractionOperation");
        fato2.setValue(String.valueOf(this.additionSubtraction));
        fato2.setCertaintyFactor(0.2);
        algorithm.addToMemoryWorking(fato2);
        
        Item fato3 = new Item();
        fato3.setDescription("SimplificationOperation");
        fato3.setValue(String.valueOf(this.simplification));
        fato3.setCertaintyFactor(0.2);
        algorithm.addToMemoryWorking(fato3);
        
        Item fato4 = new Item();
        fato4.setDescription("ExponentiationOperation");
        fato4.setValue(String.valueOf(this.exponentiation));
        fato4.setCertaintyFactor(0.2);
        algorithm.addToMemoryWorking(fato4);
        
                
        Item conclusion1 = new Item("OperationMultiplication", "true");
        String result1 = algorithm.doForwardChaining(conclusion1, false);
        
        Item conclusion2 = new Item("OperationAdditionSubtraction", "true");
        String result2 = algorithm.doForwardChaining(conclusion2, false);
        
        Item conclusion3 = new Item("OperationSimplification", "true");
        String result3 = algorithm.doForwardChaining(conclusion3, false);
        
        Item conclusion4 = new Item("OperationExponentiation", "true");
        String result4 = algorithm.doForwardChaining(conclusion4, false);
            
        if (result4.equalsIgnoreCase("OperationExponentiation")) {
        	return "Exponentiation";
        }
        
        if (result1.equalsIgnoreCase("OperationMultiplication")) {
        	return "Multiplication";
        }else{
        	if (result2.equalsIgnoreCase("OperationAdditionSubtraction")) {
            	return "AdditionSubtraction";
            }else{
            	 if (result3.equalsIgnoreCase("OperationSimplification")) {
                 	return "Simplification";
                 }
            }
        }
        return null;
	}
	
	public boolean isMultiplication() {
		return multiplication;
	}
	
	public void setMultiplication(boolean multiplication) {
		this.multiplication = multiplication;
	}
	
	public boolean isSimplification() {
		return simplification;
	}
	
	public void setSimplification(boolean simplification) {
		this.simplification = simplification;
	}

	public boolean isAdditionSubtraction() {
		return additionSubtraction;
	}

	public void setAdditionSubtraction(boolean additionSubtraction) {
		this.additionSubtraction = additionSubtraction;
	}

	public boolean isExponentiation() {
		return exponentiation;
	}

	public void setExponentiation(boolean exponentiation) {
		this.exponentiation = exponentiation;
	}
	
	
}
